package com.cater.talents.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.BaseEntity;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.talents.entity.EduExperience;
import com.cater.talents.entity.Resume;
import com.cater.talents.entity.ResumeRelationship;
import com.cater.talents.entity.WorkExperience;
import com.cater.talents.service.EduExperienceServiceImpl;
import com.cater.talents.service.ResumeRelationshipServiceImpl;
import com.cater.talents.service.ResumeServiceImpl;
import com.cater.talents.service.WorkExperienceServiceImpl;

/**
 * 描述：人脉关系控制层
 * 
 * @author yong
 * @date 2019/03/07
 */
@Controller
@RequestMapping("/relationship")
public class ResumeRelationshipController {

	@Autowired
	private ResumeRelationshipServiceImpl service;
	@Autowired
	private ResumeServiceImpl resumeService;
	@Autowired
	private WorkExperienceServiceImpl experienceService;// 工作经验
	@Autowired
	private EduExperienceServiceImpl eduExperienceService;// 教育及培训

	/**
	 * @人脉关系列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "talents/relationship/list";
	}

	/**
	 * @人脉关系数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询人脉关系分页数据")
	public PageResult<ResumeRelationship> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<ResumeRelationship> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("relationship:add")
	public String addView(String talentsId, Model model) {
		model.addAttribute("talentsId", talentsId);
		return "talents/relationship/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("relationship:add")
	@ResponseBody
	@SystemControllerLog(description = "添加人脉关系")
	public JSONObject add(ResumeRelationship t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("relationship:edit")
	public String editView(String id, Model model, CommonQuery query) {
		ResumeRelationship t = service.getById(id);
		model.addAttribute("relationship", t);
		return "talents/relationship/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("relationship:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改人脉关系")
	public JSONObject update(ResumeRelationship t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("relationship:delete")
	@SystemControllerLog(description = "删除人脉关系")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("relationship:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		ResumeRelationship t = service.getById(id);
		model.addAttribute("relationship", t);
		return "talents/relationship/detail";
	}

	@RequestMapping(value = "addBatch")
	@ResponseBody
	@SystemControllerLog(description = "添加人脉关系")
	public JSONObject addBatch(CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = query.getTalentsId();
		String tid = null;
		try {
			if (StringUtils.isNotEmpty(id)) {
				query.setType("");
				List<WorkExperience> workExperiences = experienceService.getListByTalentsId(query);
				query.setType("1");
				List<EduExperience> schools = eduExperienceService.getListByTalentsId(query);
				Resume resume = null;
				ResumeRelationship relationship = null;
				if (workExperiences != null && workExperiences.size() > 0) {
					for (WorkExperience workExperience : workExperiences) {
						String cname = workExperience.getCompanyName();
						if (StringUtils.isEmpty(cname)) {
							cname = workExperience.getCompany().getName();
						}
						query.setName(cname);
						query.setStartDate(workExperience.getStartDate());
						query.setEndDate(workExperience.getEndDate());
						List<WorkExperience> wList = experienceService.getListBy(query);
						if (wList != null && wList.size() > 0) {
							for (WorkExperience workExperience2 : wList) {
								String talentsId = workExperience2.getTalentsId();
								resume = new Resume();
								resume = resumeService.getById(talentsId);
								if (resume != null) {
									relationship = new ResumeRelationship();
									relationship.setName(resume.getTrueName());
									relationship.setResumeId(id);
									relationship.setRelationId("1");
									/**
									 * 1同事
									 */
									relationship.setResumeRelationId(talentsId);
									ResumeRelationship resumeRelationship = service.getBy(id, talentsId);
									if (resumeRelationship == null) {
										BaseEntity.getCreate(relationship);
										tid = service.saves(relationship);
									}
								}
							}
						}
					}
					if (schools != null && schools.size() > 0) {
						for (EduExperience school : schools) {
							String ename = school.getSchoolName();
							if (StringUtils.isEmpty(ename)) {
								ename = school.getSchool().getName();
							}
							query.setName(ename);
							query.setStartDate(school.getStartDate());
							query.setEndDate(school.getEndDate());
							List<EduExperience> cList = eduExperienceService.getListBy(query);
							if (cList != null && cList.size() > 0) {
								for (EduExperience eduExperience : cList) {
									String talentsId = eduExperience.getTalentsId();
									resume = new Resume();
									resume = resumeService.getById(talentsId);
									if (resume != null) {
										relationship = new ResumeRelationship();
										relationship.setName(resume.getTrueName());
										relationship.setResumeId(id);
										relationship.setRelationId("2");
										/**
										 * 1校友
										 */
										relationship.setResumeRelationId(talentsId);
										ResumeRelationship resumeRelationship = service.getBy(id, talentsId);
										if (resumeRelationship == null) {
											BaseEntity.getCreate(relationship);
											tid = service.saves(relationship);
										}
									}
								}
							}
						}
					}
				}
				if (tid != null) {
					json.put("code", 200);
					json.put("message", "一键生成成功");
				} else {
					json.put("code", 200);
					json.put("message", "暂无人脉关系");
				}
			} else {
				json.put("code", 400);
				json.put("message", "人才不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("message", "系统异常");
		}
		return json;
	}
}